<?php 
	$action = REPLACE_Injection($_GET["a"]);
	if($action == 1){
		$player = REPLACE_Injection($_POST["player"]);
		$check = REPLACE_Injection($_POST["check"]);
		$email = REPLACE_Injection($_POST["email"]);

		if(strlen($player) == 0)
			$error .= "The Player Name is not an Optional Field<br/>";
		if(strlen($player) > 14)
			$error .= "The Player Name cannot be longer than 14 characters<br/>";
		if(preg_match('/[^a-zA-Z0-9]/', $player))
			$error .= "The Player Name can only contain alphanumeric characters<br/>";		
		if($check != 1)
			$error .= "You have to accept you are not a cheater, at least. Be water my friend!";
		if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
			$error .= "Your email address is invalid";
		}
			
		if(strlen($error) == 0){
			$values = array("jugador", "isNew", "email");
			$data = array($player, 0, $email);
			$update = new mysqlUpdate("tournament", "", "usuarios", $values, $data, "`id`='".$_SESSION["user_id"]."'");
			$update->execute();
			$_SESSION["isNew"] = 0;
			$_SESSION["isValidMail"] = 0;
			sendValidationEmail();
			echo '<div id="content"><div id="ok">Player settings saved.</div><a href="index.php" class="back">Start Using PES6J Tournament</a></div>';
		}else{
			
			$query = new mysqlQuery("tournament", "", "usuarios", "*", "`id`='".$_SESSION["user_id"]."'", "", "");
			$query->execute();
			$row = $query->next();				
			?>
				<div id="content">
				<h1>Welcome to PES6J Tournament</h1>
				<div id="error">You have the following errors: <br/><?php echo $error;?></div>
				<form action="index.php?s=sessionNewPlayer&a=1" method="post">
					<table border="0" cellspacing="0" cellpadding="0" class="form">
						<tr>
							<td colspan="2">
								This is the first time you access here. We need you to set up some parameters before you start playing.<br/>
								Please, fill the following fields in order to finish your registration process.
							</td>				
						</tr>
						<tr>
							<td><strong>Forum Username</strong></td>
							<td><strong><?php echo $_SESSION["username"];?></strong></td>
						</tr>
						<tr>
							<td><strong>Location Parameters</strong></td>
							<td><?php echo $row["country"]." (".$row["continent"].")";?></td>
						</tr>
						<tr>
							<td><strong>Email Address</strong></td>
							<td><input type="text" size="30" maxlength="14" name="email" value="<?php echo $row["email"];?>"/><br/>
									This is your email address from the fourm registration. Check it carrefully. It must be <strong>validated</strong> 
									to send you information about you tournament and matches status.
							</td>
						</tr>
						<tr>
							<td><strong>Player Name</strong><br/>Most used player name in the server,<br/>The one you will use in tournaments.</td>
							<td><input type="text" size="30" maxlength="14" name="player" /></td>
						</tr>
						<tr>
							<td colspan="2">
								<input type="checkbox" value="1" name="check" /> By Registering I accept the Server Rules and the Tournament Rules
								<input type="submit" value="Save Changes" class="boton" />
							</td>				
						</tr>			
					</table>
				</form>
			</div>
			<?php
		}
			
	}else{
		$query = new mysqlQuery("tournament", "", "usuarios", "*", "`id`='".$_SESSION["user_id"]."'", "", "");
		$query->execute();
		$row = $query->next();				
?>
<div id="content">
	<h1>Welcome to PES6J Tournament</h1>
	<form action="index.php?s=sessionNewPlayer&a=1" method="post">
		<table border="0" cellspacing="0" cellpadding="0" class="form">
			<tr>
				<td colspan="2">
					This is the first time you access here. We need you to set up some parameters before you start playing.<br/>
					Please, fill the following fields in order to finish your registration process.
				</td>				
			</tr>
			<tr>
				<td><strong>Forum Username</strong></td>
				<td><strong><?php echo $_SESSION["username"];?></strong></td>
			</tr>
			<tr>
				<td><strong>Location Parameters</strong></td>
				<td><?php echo $row["country"]." (".$row["continent"].")";?></td>
			</tr>
			<tr>
				<td><strong>Email Address</strong></td>
				<td><input type="text" size="30" maxlength="14" name="email" value="<?php echo $row["email"];?>"/><br/>
						This is your email address from the fourm registration. Check it carrefully. It must be <strong>validated</strong> 
						to send you information about you tournament and matches status.
				</td>
			</tr>
			<tr>
				<td><strong>Player Name</strong><br/>Most used player name in the server,<br/>The one you will use in tournaments.</td>
				<td><input type="text" size="30" maxlength="40" name="player" /></td>
			</tr>
			<tr>
				<td colspan="2">
					<input type="checkbox" value="1" name="check" /> By Registering I accept the Server Rules and the Tournament Rules
					<input type="submit" value="Proceed to PES6J Tournament" class="boton" />
				</td>				
			</tr>			
		</table>
	</form>
</div>
<?php 
	}
?>